<?php

namespace app\services;

use app\models\ZRegion;
use app\services\base\BaseService;
use yii\helpers\ArrayHelper;

/**
 * Class User
 * @package app\services
 */
class User
{
    use BaseService;

    /**
     * Check referrer correct
     *
     * @param $user_id
     * @param $referrer_id
     * @return bool
     */
    public static function checkReferrer($user_id, $referrer_id)
    {
        $children = Chain::getChildrenAll($user_id);
        $children = ArrayHelper::getColumn($children, 'id');

        if (in_array($referrer_id, array_merge($children, [$user_id]))) {
            return true;
        }

        return false;
    }

    public static function getCity($item)
    {
        // 区代
        if ($item['address_up'] > 0) {
            $n = '省代';
            $area = ZRegion::find()
                ->where(['code' => $item['address_up']])
                ->one();
            $city_name = $area->name;
            if ($area->p_code > 0) {
                $n = '市代';
                $city = ZRegion::find()
                    ->where(['code' => $area->p_code])
                    ->one();
                $city_name = $city->name . ' ' . $city_name;
                if ($city->p_code > 0) {
                    $n = '区代';
                    $provine = ZRegion::find()
                        ->where(['code' => $city->p_code])
                        ->one();
                    $city_name = $provine->name. ' ' . $city_name;
                }
            }

            $city_name = $city_name . ' (' . $n . ')';

        } else {
            $city_name = '';
        }

        return $city_name;
    }

    public static function getCity1($item)
    {
        // 区代
        if ($item['address_up'] > 0) {
            $n = '省代';
            $area = ZRegion::find()
                ->where(['code' => $item['address_up']])
                ->one();
            $city_name = $area->name;
            if ($area->p_code > 0) {
                $n = '城市运营中心';
                $city = ZRegion::find()
                    ->where(['code' => $area->p_code])
                    ->one();
                $city_name = $city->name . ' ' . $city_name;
                if ($city->p_code > 0) {
                    $n = '县区';
                    $provine = ZRegion::find()
                        ->where(['code' => $city->p_code])
                        ->one();
                    $city_name = $provine->name. ' ' . $city_name;
                }
            }

            $city_name = $city_name . ' (' . $n . ')';

        } else {
            $city_name = '';
        }

        return $city_name;
    }

    public static function getCity2($item)
    {
        // 区代
        if ($item['address_up2'] > 0) {
            $n = '省代';
            $area = ZRegion::find()
                ->where(['code' => $item['address_up2']])
                ->one();
            $city_name = $area->name;
            if ($area->p_code > 0) {
                $n = '城市运营中心';
                $city = ZRegion::find()
                    ->where(['code' => $area->p_code])
                    ->one();
                $city_name = $city->name . ' ' . $city_name;
                if ($city->p_code > 0) {
                    $n = '县区';
                    $provine = ZRegion::find()
                        ->where(['code' => $city->p_code])
                        ->one();
                    $city_name = $provine->name. ' ' . $city_name;
                }
            }

            $city_name = $city_name . ' (' . $n . ')';

        } else {
            $city_name = '';
        }

        return $city_name;
    }
}